using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Messenger;
using System.Windows.Forms;

namespace WLMTranslator
{
	public class AddIn : Microsoft.Messenger.IMessengerAddIn
	{
		private Microsoft.Messenger.MessengerClient m_Client = null;
		private string m_currentTranslationIn = "fr|en";
		private string m_currentTranslationOut = "en|fr";
		private bool m_translationStatusOn = false;
		GoogleTranslator m_googleTranslator = new GoogleTranslator();

		public void Initialize(MessengerClient messenger)
		{
			m_Client = messenger;
			messenger = null;
			m_Client.AddInProperties.Creator = "Serialcoder";
			m_Client.AddInProperties.FriendlyName = "WLM Translator";
			m_Client.AddInProperties.Description = "Automatic Google Translator for Windows Live Messenger";

			m_Client.IncomingTextMessage += new EventHandler<Microsoft.Messenger.IncomingTextMessageEventArgs>(OnClientIncomingTextMessage);
			m_Client.OutgoingTextMessage += new EventHandler<Microsoft.Messenger.OutgoingTextMessageEventArgs>(OnClientOutgoingTextMessage);
			m_Client.Shutdown += new EventHandler(OnClientShutdown);
		}

		void OnClientShutdown(object sender, EventArgs e)
		{
			m_Client.Dispose();
			m_Client = null;
			m_googleTranslator = null;
		}

		void OnClientOutgoingTextMessage(object sender, Microsoft.Messenger.OutgoingTextMessageEventArgs e)
		{
			try
			{
				string message = e.TextMessage.ToLower();
				if (message.StartsWith("/translate"))
				{
					string command = message.Replace("/translate","").Trim();
					switch (command)
					{
						case "help":
						case "aide":
							ShowHelpMessage();
							break;
						case "list":
							string list = m_googleTranslator.GetList();
							MessageBox.Show(list, "WLMTranslator Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
							break;
						case "on":
							m_translationStatusOn = true;
							MessageBox.Show("Translation turned on", "WLMTranslator", MessageBoxButtons.OK, MessageBoxIcon.Information);
							break;
						case "off" :
							m_translationStatusOn = false;
							MessageBox.Show("Translation turned off", "WLMTranslator", MessageBoxButtons.OK, MessageBoxIcon.Information);
							break;
						default:
							if (m_googleTranslator.CodeExists(command))
							{
								m_currentTranslationIn = command;
								m_currentTranslationOut = m_googleTranslator.InvertCode(command);
								MessageBox.Show(m_googleTranslator.GetCaption(command), "WLMTranslator", MessageBoxButtons.OK, MessageBoxIcon.Information);
							}

							break;
					}
					e.Cancel = true;
					return;
				}

				if (m_translationStatusOn)
				{
					string translation = m_googleTranslator.Translate(e.TextMessage, m_currentTranslationIn);
					m_Client.SendTextMessage(translation, e.UserTo);
				}
			}
			catch (Exception ex)
			{
				m_Client.SendTextMessage(ex.Message, e.UserTo);
			}
		}

		void OnClientIncomingTextMessage(object sender, Microsoft.Messenger.IncomingTextMessageEventArgs e)
		{
			try
			{
				if (m_translationStatusOn)
				{
					string translation = m_googleTranslator.Translate(e.TextMessage, m_currentTranslationOut);
					m_Client.SendTextMessage(translation, e.UserFrom);
				}
			}
			catch (Exception ex)
			{
				m_Client.SendTextMessage(ex.Message, e.UserFrom);
			}
		}

		void ShowHelpMessage()
		{
			string help = "";
			help += "WLMTranslator Help\r\n";
			help += "\r\n";
			help += "Type on message box :\r\n";
			help += "/translate on\r\n";
			help += "--> (turn on translation)\r\n";
			help += "/translate fr|en\r\n";
			help += "--> (Tranlate from french to english)\r\n";
			help += "/translate off\r\n";
			help += "--> (turn off translation)\r\n";
			help += "/translate list\r\n";
			help += "--> (return translation list)\r\n";
			MessageBox.Show(help, "WLMTranslator Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

	}
}
